/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gtugs.domain;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

import org.springframework.util.AutoPopulatingList;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * @author jasonacooper@google.com (Jason Cooper)
 */
@PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true")
public class Chapter implements Serializable, Comparable {

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;

  @Persistent
  private String name;

  @Persistent
  private String type;

  @Persistent
  private String city;

  @Persistent
  private String state;

  @Persistent
  private String country;

  @Persistent
  private String status;

  @Persistent
  private Double latitude;

  @Persistent
  private Double longitude;

  @Persistent
  private String website;

  @Persistent
  private String mailingList;

  @Persistent(defaultFetchGroup = "true", serialized = "true")
  private List<User> administrators = new AutoPopulatingList(User.class);

  @Persistent(defaultFetchGroup = "true")
  private Text description;

  @Persistent
  private Date createdOn;

  @Persistent
  private String meetupGroupUrlName;

  @NotPersistent
  private Integer numEvents;

  @NotPersistent
  private Integer numEventsTotal;

  @NotPersistent
  private Integer averageAttendees;

  public Long getId() {
    return key.getId();
  }

  public String getName() {
    return name;
  }

  public String getType() {
    return type;
  }

  public String getCity() {
    return city;
  }

  public String getState() {
    return state;
  }

  public String getCountry() {
    return country;
  }

  public String getStatus() {
    return status;
  }

  public Double getLatitude() {
    return latitude;
  }

  public Double getLongitude() {
    return longitude;
  }

  public String getWebsite() {
    return website;
  }

  public String getMailingList() {
    return mailingList;
  }

  public List<User> getAdministrators() {
    return administrators;
  }

  public String getDescription() {
    if (description == null) {
      return "";
    }

    return description.getValue();
  }

  public Date getCreatedOn() {
    return createdOn;
  }

  public String getMeetupGroupUrlName() {
    return meetupGroupUrlName;
  }

  public Integer getNumEvents() {
    return numEvents;
  }

  public Integer getNumEventsTotal() {
    return numEventsTotal;
  }

  public Integer getAverageAttendees() {
    return averageAttendees;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setType(String type) {
    this.type = type;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public void setState(String state) {
    this.state = state;
  }

  public void setCountry(String country) {
    this.country = country;
  }

  public void setStatus(String status) {
    this.status = status;
  }

  public void setLatitude(Double latitude) {
    this.latitude = latitude;
  }

  public void setLongitude(Double longitude) {
    this.longitude = longitude;
  }

  public void setWebsite(String website) {
    this.website = website;
  }

  public void setMailingList(String mailingList) {
    this.mailingList = mailingList;
  }

  public void setAdministrators(List<User> administrators) {
    this.administrators = administrators;
  }

  public void setDescription(String description) {
    this.description = new Text(description);
  }

  public void setCreatedOn(Date createdOn) {
    this.createdOn = createdOn;
  }

  public void setMeetupGroupUrlName(String meetupGroupUrlName) {
    this.meetupGroupUrlName = meetupGroupUrlName;
  }

  public void setNumEvents(Integer numEvents) {
    this.numEvents = numEvents;
  }

  public void setNumEventsTotal(Integer numEventsTotal) {
    this.numEventsTotal = numEventsTotal;
  }

  public void setAverageAttendees(Integer averageAttendees) {
    this.averageAttendees = averageAttendees;
  }

  public int compareTo(Object o) {
    Chapter chapter = (Chapter) o;

    if (numEvents == chapter.getNumEvents()) {
      return 0;
    } else if (numEvents > chapter.getNumEvents()) {
      return 1;
    } else {
      return -1;
    }
  }
}
